<?php

namespace AppBundle\Controller;

use AppBundle\Entity\City;
use AppBundle\Entity\Country;
use AppBundle\Entity\Region;
use AppBundle\Form\LocationType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Location;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="location_list")
     * @Method("GET")
     * @Template()
     *
     * @return array
     */
    public function listLocationsAction()
    {
        return [
            'locations' => $this->getDoctrine()->getManager()->getRepository(Location::class)->findAll()
        ];
    }

    /**
     * @Route("/new", name="location_new")
     * @Method({"GET", "POST"})
     * @Template()
     *
     * @param Request $request
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function newLocationAction(Request $request)
    {
        $form = $this->createForm(LocationType::class);

        if ($form->handleRequest($request) && $form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();

            $em->persist($form->getData());
            $em->flush();

            return $this->redirectToRoute('location_list');
        }

        return [
            'form' => $form->createView()
        ];
    }

    /**
     * @Route("/edit/{id}", name="location_edit", requirements={"id"="\d+"})
     * @Method({"GET", "PUT"})
     * @Template()
     *
     * @param Request $request
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function editLocationAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $location = $em->getRepository(Location::class)->find($request->get('id'));
        if (!$location) {
            throw $this->createNotFoundException();
        }

        $form = $this->createForm(LocationType::class, $location, ['method' => 'PUT']);
        if ($form->handleRequest($request) && $form->isSubmitted() && $form->isValid()) {
            $em->flush();

            return $this->redirectToRoute('location_list');
        }

        return [
            'form' => $form->createView()
        ];
    }

    /**
     * @Route("/get_regions_for_country/{id}",
     *     name="location_get_regions_for_country",
     *     requirements={"id"="\d+"},
     *     options={"expose"=true}
     * )
     * @Method({"GET"})
     *
     * @param Country $country
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    public function getRegionsForCountry(Country $country)
    {
        return $this->json(
            $country->getRegions()->map(function(Region $region) {
                return ['id' => $region->getId(), 'name' => $region->getName()];
            })->toArray()
        );
    }

    /**
     * @Route("/get_cities_for_region/{id}",
     *     name="location_get_cities_for_region",
     *     requirements={"id"="\d+"},
     *     options={"expose"=true}
     * )
     * @Method({"GET"})
     *
     * @param Region $region
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    public function getCitiesForRegion(Region $region)
    {
        return $this->json(
            $region->getCities()->map(function(City $city) {
                return ['id' => $city->getId(), 'name' => $city->getName()];
            })->toArray()
        );
    }
}
